package com.redare.dev.smart.generator.service.service;

import com.redare.dev.smart.generator.sdk.pojo.OnlFormField;
import com.redare.dev.smart.generator.sdk.pojo.OnlFormHead;
import com.redare.dev.smart.generator.sdk.pojo.form.OnlFormFieldForm;
import com.redare.dev.smart.generator.sdk.pojo.form.OnlFormHeadForm;
import com.redare.dev.smart.generator.sdk.utils.StringUtils;
import com.redare.dev.smart.generator.service.dao.OnlFormFieldDao;
import com.redare.dev.smart.generator.service.dao.OnlFormHeadDao;
import com.redare.devframework.common.pojo.Page;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

;


/**
 * impl
 *
 * @date 2021-08-28 11:44:05
 * Created by yangqiangyu
 */
@Service(value = "com.redare.dev.smart.generator.service.OnlFormHeadService")
public class OnlFormHeadService {

    @Autowired
    OnlFormHeadDao onlFormHeadDao;

    @Autowired
    OnlFormFieldDao formFieldDao;


    public boolean addOnlFormHead(OnlFormHeadForm.Add form) {
        long id = onlFormHeadDao.addOnlFormHead(form);
        return id > 0;
    }


    @Transactional
    public boolean addOnlFormHeadByBean(List<OnlFormHead> list) {
        for (OnlFormHead formHead : list) {
            long id = onlFormHeadDao.addOnlFormHeadByBean(formHead);

            List<OnlFormField> fields = formHead.getFields();
            if (CollectionUtils.isNotEmpty(fields)){
                for (OnlFormField field : fields) {
                    field.setFormHeadId(id);
                }
                formFieldDao.addOnlFormFieldByBean(fields);
            }
        }
        return true;
    }


    @Transactional(rollbackFor = Exception.class)
    public boolean editOnlFormHead(OnlFormHeadForm.Edit form) {
        int i = onlFormHeadDao.editOnlFormHead(form);
        if (i > 0){
            List<OnlFormField> formFields = form.getFormFields();
            if (CollectionUtils.isNotEmpty(formFields)) {
                for (int i1 = 0; i1 < formFields.size(); i1++) {
                    OnlFormField formField = formFields.get(i1);
                    formField.setOrderNum(i1);
                    if (formField.getId()!=null){
                        formFieldDao.editOnlFormFieldByBean(formField);
                    }else{
                        String fieldType = formField.getFieldType();
                        if (StringUtils.isNotBlank(fieldType)){
                            switch (fieldType) {
                                case "Long":
                                    formField.setDbType("bigint");
                                case "Integer":
                                    formField.setDbType("int");
                                case "String":
                                    formField.setDbType("varchar");
                                case "Double":
                                    formField.setDbType("double");
                                case "java.util.Date":
                                    formField.setDbType("datetime");
                                case "Text":
                                    formField.setDbType("text");
                                case "BigDecimal":
                                    formField.setDbType("bigdecimal");
                            }
                        }
                        formField.setFormHeadId(form.getId());
                        formFieldDao.addOnlFormFieldByBean(Arrays.asList(formField));
                    }
                    //附表信息处理
                    if (form.getTableType()==2&&StringUtils.isNotBlank(formField.getMainTable())&&StringUtils.isNotBlank(formField.getMainField())){
                        OnlFormHead onlFormHead = onlFormHeadDao.getOnlFormHead(new OnlFormHeadForm.Get().setTableName(formField.getMainTable()));
                        if (onlFormHead != null) {
                            String subTableStr = "";
                            if (StringUtils.isBlank(onlFormHead.getSubTableStr())){
                                subTableStr = form.getTableName();
                            }else{
                                if (!onlFormHead.getSubTableStr().contains(form.getTableName())){
                                    subTableStr = onlFormHead.getSubTableStr();
                                    subTableStr += ",";
                                    subTableStr += form.getTableName();
                                }
                            }
                            onlFormHeadDao.editOnlFormHead(new OnlFormHeadForm.Edit().setId(onlFormHead.getId())
                            .setSubTableStr(subTableStr).setTableType(1));
                        }
                    }
                }
            }
        }
        return true;
    }

    public boolean deleteOnlFormHead(OnlFormHeadForm.Del form) {
        return onlFormHeadDao.deleteOnlFormHead(form) > 0;
    }

    public List<OnlFormHead> findOnlFormHeadList(OnlFormHeadForm.Search form) {
        List<OnlFormHead> list = onlFormHeadDao.findOnlFormHeadPage(form, 1, 20, false, true).getResult();
        if (CollectionUtils.isNotEmpty(list)) {
            for (OnlFormHead formHead : list) {
                List<OnlFormField> fieldList = formFieldDao.findOnlFormFieldPage(new OnlFormFieldForm.Search().setFormHeadId(formHead.getId()), 1, 1, false, true).getResult();
                formHead.setFields(fieldList);
            }
        }
        return list;
    }

    public List<OnlFormHead> findOnlFormHeadList(OnlFormHeadForm.Search form, int curPage, int pageSize) {
        return onlFormHeadDao.findOnlFormHeadPage(form, curPage, pageSize, false, false).getResult();
    }

    public Page<OnlFormHead> findOnlFormHeadPage(OnlFormHeadForm.Search form, int curPage, int pageSize) {
        return onlFormHeadDao.findOnlFormHeadPage(form, curPage, pageSize, true, false);
    }

    public OnlFormHead getOnlFormHead(OnlFormHeadForm.Get form) {
        OnlFormHead onlFormHead = onlFormHeadDao.getOnlFormHead(form);
        if (onlFormHead!=null&&form.isQueryField()){
            List<OnlFormField> result = formFieldDao.findOnlFormFieldPage(new OnlFormFieldForm.Search().setFormHeadId(form.getId()), 1, 1, false, true).getResult();
            onlFormHead.setFields(result);
        }
        return onlFormHead;

    }


}

